package com.time.workshop.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.time.workshop.ExitManager;
import com.time.workshop.R;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.ui.fragment.BuyTimeFragment;
import com.time.workshop.ui.fragment.HeadFragment;
import com.time.workshop.ui.fragment.MenuFragment;
import com.time.workshop.ui.fragment.MineFragment;
import com.time.workshop.ui.fragment.TimeHallFragment;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.ImageUtil;
import com.time.workshop.utils.NewToast;
import com.umeng.update.UmengUpdateAgent;

public class MainActivity extends SlidingFragmentActivity
{

	public View rootView;
	private HeadFragment headFragment;
	private BuyTimeFragment workShopFragment;
	private MineFragment myFragment;
	private MenuFragment menuFragment;
	private TimeHallFragment timeHallFragment;

	@V
	private ImageView img_shadow;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// baseView.setTitleView(this, R.layout.base_view_titleleft,
		// R.layout.base_view_titlecenter_with_logo,
		// R.layout.base_view_titleright_main,
		// R.layout.activity_slidingmenu);
		// rootView = baseView.getTitleView();
		// setContentView(rootView);
		setContentView(R.layout.activity_slidingmenu);
		setView();
		UmengUpdateAgent.setDefault();
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);
		UmengUpdateAgent.setUpdateCheckConfig(false);
		ImageUtil.delete(CommonUtils.getExternalCacheDir(MainActivity.this));
	}

	private void setView()
	{
		Injector.getInstance().inject(this);
		if (null == headFragment)
		{
			headFragment = new HeadFragment();
		}
		if (null == workShopFragment)
		{
			workShopFragment = new BuyTimeFragment();
		}
		if (null == myFragment)
		{
			myFragment = new MineFragment();
		}
		if (null == menuFragment)
		{
			menuFragment = new MenuFragment();
		}
		if (null == timeHallFragment)
		{
			timeHallFragment = new TimeHallFragment();
		}
		// btn_fragment_left
		// .setBackgroundResource(R.drawable.btn_title_left_bg_selector);
		// btn_fragment_left.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// menu.showMenu();
		// }
		// });
		menu = getSlidingMenu();
		menu.setMode(SlidingMenu.LEFT);// 设置侧边栏菜单为左模式
		menu.setBehindWidthRes(R.dimen.width_30_80);// 设置左边菜单的宽度,该值为左菜单展开的宽度
		menu.setShadowDrawable(R.drawable.shadow);// 设置左菜单的阴影
		menu.setShadowWidth(10);// 设置阴影宽度
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);// 设置侧边栏菜单触摸模式为左边模式（0：左边；1：全屏；2：不滑动）
		setBehindContentView(R.layout.slidingmenu); // 设置左菜单的默认VIEW

		getSupportFragmentManager().beginTransaction().add(R.id.menu_frame, menuFragment).replace(R.id.basecontent, headFragment).commit(); // 将左菜单默认VIEW替换为左菜单Fragment
		// menu.setOnOpenListener(new OnOpenListener() {
		//
		// @Override
		// public void onOpen() {
		// menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		// }
		// });
		// menu.setOnClosedListener(new OnClosedListener() {
		//
		// @Override
		// public void onClosed() {
		// menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		// }
		// });

	}

	public void showContent()
	{
		if (menu.isMenuShowing())
		{
			new Handler().postDelayed(new Runnable()
			{
				@Override
				public void run()
				{
					menu.toggle(false);
				}
			}, 50);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
		{
			if (getSupportFragmentManager().getBackStackEntryCount() == 0)
			{
				// if (curFragment instanceof IndexFragment) {
				if (!CommonUtils.isFastDoubleClick(3600))
				{
					NewToast.makeText(context, R.string.Class305);
				} else
				{
					ExitManager.getInstance().exit();
					// back();
				}
				return true;
			} else
			{
				getSupportFragmentManager().popBackStack();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2)
	{
		super.onActivityResult(arg0, arg1, arg2);
	}

}
